package com.demo.servicelmpl;

import com.demo.bean.ConflictObjects;
import com.demo.bean.FixedEvent;
import com.demo.mapper.ResultMapper;
import com.demo.mapper.TaskMapper;
import com.demo.service.ResultService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author 李天航
 * 操作日程的实现类
 */
@Service
public class ResultServiceImpl implements ResultService {

    @Resource
    ResultMapper resultMapper;
    @Resource
    TaskMapper taskMapper;

    private ConflictObjects judgeConflict(FixedEvent fixedEvent)
    {
        FixedEvent fixedEvent1=resultMapper.firstLargerBeginTime(fixedEvent);
        if(fixedEvent1!=null&&fixedEvent.getEndTime()!=null&&fixedEvent.getEndTime()>fixedEvent1.getBeginTime()) {
            fixedEvent1.setFrom("活动安排");
            fixedEvent.setFrom("活动安排");
            return new ConflictObjects(false,fixedEvent1,fixedEvent);
        }
        FixedEvent fixedEvent2=resultMapper.firstLargerEndTime(fixedEvent);
        if(fixedEvent2!=null&&fixedEvent.getBeginTime()!=null&&fixedEvent2.getBeginTime()<fixedEvent.getEndTime()) {
            fixedEvent2.setFrom("活动安排");
            fixedEvent.setFrom("活动安排");
            return new ConflictObjects(false,fixedEvent2,fixedEvent);
        }
        return new ConflictObjects(true,null,null);
    }

    @Override
    public LinkedHashMap<String, Object> addResult(FixedEvent fixedEvent) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            if(fixedEvent.getBeginTime()> fixedEvent.getEndTime())
            {
                resultMap.put("status",500);
                resultMap.put("msg","起始时间不能大于终止时间");
                return resultMap;
            }
            ConflictObjects conflictObjects=judgeConflict(fixedEvent);
            if(!conflictObjects.isNotConflict)
            {
                resultMap.put("status",500);
                resultMap.put("msg","活动与其他活动冲突");
                resultMap.put("addResult",conflictObjects.rawObject);
                resultMap.put("conflictResult",conflictObjects.newObject);
            }
            else
            {
                resultMapper.addResult(fixedEvent);
                resultMap.put("status", 200);
                resultMap.put("msg", "添加成功");
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }

    @Override
    public LinkedHashMap<String, Object> updateResult(FixedEvent fixedEvent) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            if(fixedEvent.getBeginTime()==null&&fixedEvent.getEndTime()==null)
            {
                resultMapper.updateResult(fixedEvent);
                resultMap.put("status", 200);
                resultMap.put("msg", "修改成功");
                return resultMap;
            }
            FixedEvent rawFixedEvent=resultMapper.getResultById(fixedEvent.getEventId());
            if(fixedEvent.getUserAccount().equals(rawFixedEvent.getUserAccount())) {
                if(fixedEvent.getBeginTime()!=null) {
                    rawFixedEvent.setBeginTime(fixedEvent.getBeginTime());
                }
                if(fixedEvent.getEndTime()!=null) {
                    rawFixedEvent.setEndTime(fixedEvent.getEndTime());
                }
                if(rawFixedEvent.getBeginTime()> rawFixedEvent.getEndTime())
                {
                    resultMap.put("status",500);
                    resultMap.put("msg","起始时间不能大于终止时间");
                    return resultMap;
                }
                ConflictObjects conflictObjects=judgeConflict(rawFixedEvent);
                if(conflictObjects.isNotConflict) {
                    resultMapper.updateResult(fixedEvent);
                    resultMap.put("status", 200);
                    resultMap.put("msg", "修改成功");
                }
                else
                {
                    resultMap.put("status",500);
                    resultMap.put("msg","活动与其他活动冲突");
                    resultMap.put("updateResult",conflictObjects.rawObject);
                    resultMap.put("conflictResult",conflictObjects.newObject);
                }
            }
            else
            {
                resultMap.put("status", 500);
                resultMap.put("msg", "无修改权限");
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }

    @Override
    public LinkedHashMap<String, Object> deleteResult(Integer resultId, String userAccount) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            FixedEvent result=resultMapper.getResultById(resultId);
            if(!result.getUserAccount().equals(userAccount))
            {
                resultMap.put("status",500);
                resultMap.put("msg","无修改权限");
            }
            else {
                resultMapper.deleteResult(resultId);
                resultMap.put("status", 200);
                resultMap.put("msg", "删除成功");
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }

    @Override
    public LinkedHashMap<String, Object> getResults(FixedEvent fixedEvent) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            resultMap.put("results",resultMapper.getResultsByUserAccountAndWeekNum(fixedEvent));
            resultMap.put("status",200);
            resultMap.put("msg","查询成功");
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public LinkedHashMap<String, Object> finishResult(Integer resultId, String userAccount) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        FixedEvent fixedEvent=resultMapper.getResultById(resultId);
        resultMapper.finishFixedEventByResultId(resultId);
        if(fixedEvent.getTaskId()!=-1) {
            taskMapper.finishTask(fixedEvent.getTaskId());
        }
        resultMap.put("status",200);
        resultMap.put("msg","打卡成功");
        return resultMap;
    }

    /**
     * 取消打卡
     *
     * @param resultId    结果变化
     * @param userAccount 用户账号
     * @return status, msg
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public LinkedHashMap<String, Object> unfinishResult(Integer resultId, String userAccount) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        FixedEvent fixedEvent=resultMapper.getResultById(resultId);
        resultMapper.unfinishFixedEventByResultId(resultId);
        if(fixedEvent.getTaskId()!=-1) {
            taskMapper.unfinishTask(fixedEvent.getTaskId());
        }
        resultMap.put("status",200);
        resultMap.put("msg","打卡成功");
        return resultMap;
    }

    /**
     * 计算间隔
     * @param results 活动表
     * @return 时间间隔
     */
    Long maxIntervalTime(List<FixedEvent> results)
    {
        long maxIntervalTime= -1L;
        for(int i=0;i+1<results.size();i++)
        {
            maxIntervalTime=Math.max(maxIntervalTime,
                    results.get(i+1).getBeginTime()-results.get(i).getEndTime());
        }
        return maxIntervalTime;
    }

    /**
     * 能安排的活动的最大时间间隔
     * @param fixedEvent userAccount,weekNum
     * @return 最大时间间隔
     */
    @Override
    public Long maxTaskTime(FixedEvent fixedEvent) {
        List<FixedEvent> results=resultMapper.getResultsByUserAccountAndWeekNum(fixedEvent);
        return maxIntervalTime(results);
    }

}
